package CorpusConverter;

import java.util.ArrayList;
import java.lang.Math;
import java.util.Collections;

/**
 * @author Robert Pethes
 * 
 * A class for treating NGrams. NGrams are n-tuples of part-of-speech-tags that are used for refining the language model. 
 *
 */
public class NGram extends Tag {
	
	private ArrayList<String> _tags;
	
	/**
	 * Creates an NGram from given tags.
	 * 
	 * @param tags the POS-tags that form the NGram.
	 */
	public NGram(ArrayList<String> tags){
		_tags = tags;
	}
	
	public int compareTo(Object o) {
		
		if (o==this)
			return 0;
		
		NGram theOther = (NGram) o;
		
		int n = Math.min(getN(), theOther.getN());
		int ret = 0;
		
		for (int i = 0; i < n; i++){
			
			ret = _tags.get(i).compareTo(theOther._tags.get(i));
			
			if (ret!=0)
				return ret;
		}
	
		return 0;
	}
	
	/**
	 * @return the N parameter of the NGram model.
	 */
	public int getN() {
		return _tags.size();
	}
	
	/**
	 * Shifts the tag list to the left and appends the given string to it. 
	 * 
	 * @param s the string to append.
	 */
	public void push_back(String s){
		Collections.rotate(_tags,-1);
		_tags.set(_tags.size()-1, s);
	}
	
	public NGram clone() {
		ArrayList<String> copy = new ArrayList<String>(_tags);
		return new NGram(copy);
	}
	
	/**
	 * Returns the i-th tag of the NGram.
	 * 
	 * @param i the index of the tag to be returned.
	 * @return the i-th tag.
	 */
	public String getTag(int i) {
		if (i>=_tags.size()) 
			return null;
		else
			return _tags.get(i);
	}
}
